namespace Sitecore.ChangesTracker.UI
{
  using System;
  using System.Collections.Generic;
  using ComponentArt.Web.UI;
  using Sitecore;
  using Common;
  using Data.Archiving;
  using Data.Items;
  using Diagnostics;
  using Security.Accounts;
  using Shell.Applications.WebEdit;
  using Shell.Framework.Commands;
  using Sitecore.ChangesTracker.Archiving;
  using Web;
  using Web.UI.Grids;
  using Web.UI.XamlSharp.Xaml;
  using System.Linq;


  public class RecycleBinPageEx : XamlMainControl, IHasCommandContext
  {
    public string ArchiveName
    {
      get
      {
        return StringUtil.GetString(this.ViewState["ArchiveName"]);
      }

      set
      {
        Assert.ArgumentNotNull(value, "value");
        this.ViewState["ArchiveName"] = value;
      }
    }

    protected Grid Items { get; set; }

    CommandContext IHasCommandContext.GetCommandContext()
    {
      string str = "/sitecore/content/Applications/Archives/Restore/Ribbon";
      Item itemNotNull = Client.GetItemNotNull(str, Client.CoreDatabase);
      CommandContext context = new CommandContext();
      context.Parameters["item"] = GridUtil.GetSelectedValue("Items");
      context.Parameters["archivename"] = this.ArchiveName;
      context.RibbonSourceUri = itemNotNull.Uri;
      return context;
    }

    protected override void OnInit([NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(e, "e");     
      base.OnInit(e);
    }

    protected override void OnLoad([NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(e, "e");
      base.OnLoad(e);
      this.ArchiveName = StringUtil.GetString(new string[] { WebUtil.GetQueryString("an", "recyclebin") });
      
          Assert.CanRunApplication("Archives/Restore");
      
    
      IPageable<ChangeTrackerArchiveEntry> archiveEntries = this.GetArchiveEntries();
     
     ComponentArtGridHandler<ChangeTrackerArchiveEntry>.Manage(this.Items, new GridSource<ChangeTrackerArchiveEntry>(archiveEntries), !XamlControl.AjaxScriptManager.IsEvent);
    }

    private IPageable<ChangeTrackerArchiveEntry> GetArchiveEntries()
    {
      Sitecore.Data.Archiving.Archive archive;
      archive = this.ArchiveName == "changetracker" ? ArchiveManager.Providers[this.ArchiveName].GetArchive(this.ArchiveName, Sitecore.Context.ContentDatabase) : ArchiveManager.GetArchive(this.ArchiveName, Sitecore.Context.ContentDatabase);

      if (archive == null)
      {
        return Assert.ResultNotNull<Pageable<ChangeTrackerArchiveEntry>>(Pageable<ChangeTrackerArchiveEntry>.Empty);
      }

      User user = Sitecore.Context.User;
      return new Pageable<ChangeTrackerArchiveEntry>(
          delegate(int pageIndex, int pageSize)
          {
            return (IEnumerable<ChangeTrackerArchiveEntry>)((Archiving.ChangeTrackerArchive)archive).GetDateEntriesForUser(user, pageIndex, pageSize, WebUtil.GetSessionDateTime("cht_sdate", DateTime.Today), WebUtil.GetSessionDateTime("cht_edate", DateTime.Now)).ToList();
          }
          ,
      delegate
      {
        return (IEnumerable<ChangeTrackerArchiveEntry>)((Archiving.ChangeTrackerArchive)archive).GetDateEntriesForUser(user, 0, 0x7fffffff, WebUtil.GetSessionDateTime("cht_sdate", DateTime.Today), WebUtil.GetSessionDateTime("cht_edate", DateTime.Now)).ToList();
      },
      delegate
      {
        return  ((Archiving.ChangeTrackerArchive)archive).GetEntryCountForUser(user);
      });
    }
   
  }
}
